package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import io.netty.util.internal.ObjectUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"createBy",String.class,getLoginUser());
        this.strictInsertFill(metaObject,"createTime",Date.class,new Date());
        this.setFieldValByName("updateBy",getLoginUser(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy",getLoginUser(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    public String getLoginUser() {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        if (!ObjectUtils.isEmpty(loginUser)) {
            return loginUser.getUserId().toString();
        }
        return "1";
    }
}